package net.sashag.nadlandroid;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;

public class GestureDetectingOverlay extends Overlay implements OnGestureListener {
	
	public interface OnTapListener {
		void onTap(float x, float y);
	}
	
	private GestureDetector gestureDetector;
	private OnGestureListener onGestureListener;
	private OnTapListener onTapListener;
	
	public GestureDetectingOverlay(OnTapListener tapListener, OnGestureListener gestureListener) {
		gestureDetector = new GestureDetector(this);
		onTapListener = tapListener;
		onGestureListener = gestureListener;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		if (gestureDetector.onTouchEvent(event)) {
			return true;
		}
		return false;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		if (onGestureListener != null) {
			return onGestureListener.onDown(e);
		}
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if (onGestureListener != null) {
			return onGestureListener.onFling(e1, e2, velocityX, velocityY);
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		if (onGestureListener != null) {
			onGestureListener.onLongPress(e);
		}
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		if (onGestureListener != null) {
			onGestureListener.onScroll(e1, e2, distanceX, distanceY);
		}
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		if (onGestureListener != null) {
			onGestureListener.onShowPress(e);
		}
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		if (onGestureListener != null) {
			onGestureListener.onSingleTapUp(e);
		}
		if (onTapListener != null) {
			onTapListener.onTap(e.getX(), e.getY());
		}
		return false;
	}
}